<?php

namespace StarCloud\Library\Sdk;

use Framework\DiServices\HttpClient;
use Phalcon\Di;
use StarCloud\Library\Exception;
use StarCloud\Models\Doumvip\AppCookie;

class Kasu
{
    /* @var $httpClient HttpClient */
    private $httpClient;

    private $appId = 'HsQXvuRJUBmq1Py0lSakrEGTg5twxzfI';
    private $apiKey = 'MykKrR2iF1pv43GQbNZEhTSC5Pu0jHns';
    private $payPassword = '582071';

    private $order_call_url = 'https://api.doumvip.com/api/kasu/third_order_callback';

//    protected $domain = 'https://api.vysc.vip';
    protected $domain = 'https://hmapi.vysc.vip';

    protected $cacheClass = null;


    public function __construct($config = [])
    {
        $this->httpClient = Di::getDefault()->get('httpClient');
        $this->cacheClass = Di::getDefault()->get('redis');
    }

    public function verifyCallBackSign($post)
    {
        $sign = $post['sign'] ?? '';
        unset($post['sign']);
        //卡密和物流信息 回调不签名
        if (isset($post['card_list'])) {
            unset($post['card_list']);
        }
        if (isset($post['express_list'])) {
            unset($post['express_list']);
        }
        ksort($post); //排序post参数
        try {
            $newsign = sha1($post['time'] . json_encode($post, 256) . $this->payPassword);//签名
        } catch (\Throwable $e) {

        }
        return !empty($newsign) && $newsign == $sign;
    }

    public function sign($post = [])
    {
        if ($post) {
            ksort($post); //排序post参数
            $post = json_encode($post , JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        } else {
            $post = "{}";
        }
        $header = [];
        $time = time() . rand(100, 999);
        $header['Content-Type'] = "application/json; charset=utf-8";
        //用户密钥
        $header['Sign'] = sha1($time . $post . $this->apiKey);
        $header['Timestamp'] =  $time;
        //用户ID
        $header['UserId'] =  $this->appId;
        return [$post, $header];
    }
    public function getCategory()
    {
        $keyCate = 'kasu_category';
        $cacheResult = $this->cacheClass->get($keyCate);
        if (!empty($cacheResult)) {
            return json_decode($cacheResult, true);
        }
        $url = $this->domain . '/api/v1/goods/cate';
        $body = [];
        list($bodyJson, $header) = $this->sign($body);
        $response = $this->httpClient->sendPostRequest($url, $body, $header);

        $result =  json_decode($response->getBody()->getContents(), true);

        $this->cacheClass->set($keyCate, json_encode($result), 3800);
        return $result;
    }

    public function getGoodsList($cateId = '', $keyword = '', $page = 1, $size  = 10)
    {
        $url = $this->domain . '/api/v1/goods/list';
        $body = [
            'cate_id' => $cateId,
            'keyword' => $keyword,
            'page' => $page,
            'limit' => $size,
        ];
        list($bodyJson, $header) = $this->sign($body);
        $response = $this->httpClient->sendPostRequest($url, $body, $header);
        return json_decode($response->getBody()->getContents(), true);
    }

    public function getGoodsDetail($goodsId)
    {
        $goodsKey = "kasu_{$goodsId}";
        $cacheResult = $this->cacheClass->get($goodsKey);
        if (!empty($cacheResult)) {
            return json_decode($cacheResult, true);
        }
        $url = $this->domain . '/api/v1/goods/info';
        $body = [
            'id' => $goodsId,
        ];
        list($bodyJson, $header) = $this->sign($body);
        $response = $this->httpClient->sendPostRequest($url, $body, $header);
        $result = json_decode($response->getBody()->getContents(), true);

        if (isset($result['data'])) {
            $this->cacheClass->set($goodsKey, json_encode($result), 7200);

        }
        return $result;
    }

    public function createOrder($goodsId, $orderNo, $safe_price, $quantity, $mark)
    {
        $url = $this->domain . '/api/v1/order/buy';
        $body = [
            'id' => $goodsId,
            'url' => $this->order_call_url,
            'external_orderno' => $orderNo,
            'safe_price' => $safe_price,
            'quantity' => $quantity,
            'mark' => $mark,
        ];
        list($bodyJson, $header) = $this->sign($body);
        $response = $this->httpClient->sendPostRequest($url, $body, $header);
        return json_decode($response->getBody()->getContents(), true);
    }

    public function queryOrder($orderSn)
    {
        $url = $this->domain . '/api/v1/order/info';
        $body = [
            'ordersn' => $orderSn,
            'day' => 0,
        ];
        list($bodyJson, $header) = $this->sign($body);
        $response = $this->httpClient->sendPostRequest($url, $body, $header);
        return json_decode($response->getBody()->getContents(), true);
    }
}